﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.IO;

namespace Steve.TestHelper
{
    [Export(typeof(SiteExplorerRepository))]
    public class StubSiteExplorerRepository : SiteExplorerRepository
    {
        public static Dictionary<Guid, SiteUrl> _urls = new Dictionary<Guid,SiteUrl>();
        public static Dictionary<Guid, RewriteUrl> _rewrites = new Dictionary<Guid, RewriteUrl>();
        public static Dictionary<string, Site> _sites = new Dictionary<string, Site>();

        public static void Clear()
        {
            _urls.Clear();
            _rewrites.Clear();
            _sites.Clear();
        }

        public static void AddSiteAlt(string siteName, string authority)
        {
            var alts = _sites[siteName].AlternateAuthorities;

            if (alts == null) alts = new string[] { };

            _sites[siteName].AlternateAuthorities = alts.Concat(new string[] { authority }).ToList();
        }

        public static Site AddSite(string siteName, string authority, bool isManaged = true)
        {
            var s = new Site()
            {
                SiteName = siteName,
                Authority = authority,
                IsManagedSite = isManaged
            };

            _sites.Add(siteName, s);

            return s;
        }

        public static RewriteUrl AddRewrite(Guid urlId, string siteName, string path, DateTime? start = null, DateTime? end = null, bool secure = false, bool ignoreQueryString = true)
        {
            var r = new RewriteUrl()
            {
                Id = Guid.NewGuid(),
                SiteUrlId = urlId,
                Url = path,
                SiteName = siteName,
                Start = start,
                End = end,
                IsSecure = secure,
                IgnoreQueryString = ignoreQueryString
            };

            _rewrites.Add(r.Id, r);

            return r;
        }

        public static SiteUrl AddUrl(string siteName, string title, string path, DateTime? start = null, DateTime? end = null, bool secure = false, bool ignoreQueryString = true)
        {
            var u = new SiteUrl()
            {
                Id = Guid.NewGuid(),
                PageTitle = title,
                Url = path,
                SiteName = siteName,
                Start = start,
                End = end,
                IsSecure = secure,
                IgnoreQueryString = ignoreQueryString
            };

            _urls.Add(u.Id, u);

            return u;
        }

        protected override IEnumerable<Site> GetSitesCore()
        {
            return _sites.Values;
        }

        protected override Site SaveSiteCore(Site toSave)
        {
            _sites[toSave.SiteName] = toSave;

            return toSave;
        }

        public override SiteUrl GetSiteUrl(Guid id)
        {
            return _urls[id];
        }

        protected override SiteUrl SaveSiteUrlCore(SiteUrl toSave)
        {
            _urls[toSave.Id] = toSave;

            return toSave;
        }

        public override IEnumerable<SiteUrl> GetSiteUrls(string siteName, DateTime? liveUntil)
        {
            IEnumerable<SiteUrl> filtered = _urls.Values;

            if (liveUntil != null)
            {
                filtered = filtered.Where(u => (u.End == null || u.End.Value >= liveUntil.Value) && (u.Start == null || u.Start.Value <= liveUntil));
            }

            var query = from u in filtered
                        where u.SiteName == siteName
                        select u;

            return query.ToList();
        }

        public override IEnumerable<RewriteUrl> GetRewrites(string siteName, DateTime? liveUntil)
        {
            IEnumerable<RewriteUrl> filtered = _rewrites.Values;

            if (liveUntil != null)
            {
                filtered = filtered.Where(u => (u.End == null || u.End.Value >= liveUntil.Value) && (u.Start == null || u.Start.Value <= liveUntil));
            }

            var query = from r in filtered
                        where r.SiteName == siteName
                        select r;

            return query.ToList();
        }

        public override IEnumerable<RewriteUrl> GetRewritesForSiteUrl(Guid siteUrlId)
        {
            var query = from r in _rewrites.Values
                        where r.SiteUrlId == siteUrlId
                        select r;

            return query;
        }

        protected override RewriteUrl SaveUrlRewriteCore(RewriteUrl rewriteUrl)
        {
            _rewrites[rewriteUrl.Id] = rewriteUrl;

            return rewriteUrl;
        }

        public override RewriteUrl GetRewriteUrl(Guid id)
        {
            return _rewrites[id];
        }

    }
}
